package org.dromara.farmer.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.farmer.domain.bo.InsuranceFarmerBo;
import org.dromara.farmer.domain.vo.InsuranceFarmerVo;
import org.dromara.farmer.domain.InsuranceFarmer;
import org.dromara.farmer.mapper.InsuranceFarmerMapper;
import org.dromara.farmer.service.IInsuranceFarmerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 农户查询Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-12
 */
@RequiredArgsConstructor
@Service
public class InsuranceFarmerServiceImpl implements IInsuranceFarmerService {

    private final InsuranceFarmerMapper baseMapper;

    /**
     * 查询农户查询
     *
     * @param id 主键
     * @return 农户查询
     */
    @Override
    public InsuranceFarmerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询农户查询列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 农户查询分页列表
     */
    @Override
    public TableDataInfo<InsuranceFarmerVo> queryPageList(InsuranceFarmerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InsuranceFarmer> lqw = buildQueryWrapper(bo);
        Page<InsuranceFarmerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的农户查询列表
     *
     * @param bo 查询条件
     * @return 农户查询列表
     */
    @Override
    public List<InsuranceFarmerVo> queryList(InsuranceFarmerBo bo) {
        LambdaQueryWrapper<InsuranceFarmer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<InsuranceFarmer> buildQueryWrapper(InsuranceFarmerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InsuranceFarmer> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFarmerName()), InsuranceFarmer::getFarmerName, bo.getFarmerName());
        lqw.eq(StringUtils.isNotBlank(bo.getFarmerIdNumber()), InsuranceFarmer::getFarmerIdNumber, bo.getFarmerIdNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhone()), InsuranceFarmer::getContactPhone, bo.getContactPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), InsuranceFarmer::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getInsuranceType()), InsuranceFarmer::getInsuranceType, bo.getInsuranceType());
        lqw.eq(bo.getInsuredAmount() != null, InsuranceFarmer::getInsuredAmount, bo.getInsuredAmount());
        lqw.eq(bo.getInsuredDate() != null, InsuranceFarmer::getInsuredDate, bo.getInsuredDate());
        lqw.eq(StringUtils.isNotBlank(bo.getPolicyNumber()), InsuranceFarmer::getPolicyNumber, bo.getPolicyNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginIp()), InsuranceFarmer::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null, InsuranceFarmer::getLoginDate, bo.getLoginDate());
        return lqw;
    }

    /**
     * 新增农户查询
     *
     * @param bo 农户查询
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(InsuranceFarmerBo bo) {
        InsuranceFarmer add = MapstructUtils.convert(bo, InsuranceFarmer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改农户查询
     *
     * @param bo 农户查询
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(InsuranceFarmerBo bo) {
        InsuranceFarmer update = MapstructUtils.convert(bo, InsuranceFarmer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(InsuranceFarmer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除农户查询信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
